
(function () {
    var core = ecui,
        util = core.util,
        ui = ecui.ui,
        dom = ecui.dom,
        detail = {},
        fromEdit = null;

    Object.assign(
        NS.data,
        {
        }
    );
    Object.assign(
        NS.ui,
        {
            TypeSelectEdit: ecui.inherits(
                mao.ui.SelectEdit,
                {
                    onchange: function (event) {
                        mao.ui.SelectEdit.prototype.onchange.call(this, event);
                        this.changeHandler(event);
                    },
                    changeHandler: function (event) {
                        var num = this.$ID.split('-').pop(),
                            value = +this.getValue(),
                            checkCode = ecui.get('check-code-' + num),
                            number = ecui.get('number-' + num),
                            code = ecui.get('code-' + num),
                            date = ecui.get('date-' + num),
                            preTaxAmount = ecui.get('pre-tax-amount-' + num),
                            amount = ecui.get('amount-' + num),
                            tax = ecui.get('tax-' + num);
        
                        var showNumber = [1, 4, 10, 11, 14, 20, 31, 32].indexOf(value) > -1;
                        var showCheckCode = [1, 4, 10, 11, 14, 20].indexOf(value) > -1;
                        var showAmount = [31, 32].indexOf(value) > -1;
                        var showPreTaxAmount = [1, 20].indexOf(value) > -1;
                        var showTax = [31, 32, 1, 20].indexOf(value) > -1;
        
                        // 控制 发票号码 显示
                        number[showNumber ? 'enable' : 'disable']();
                        number.getParent()[showNumber ? 'show' : 'hide']();
                        // 控制 校验码 显示
                        checkCode[showCheckCode ? 'enable' : 'disable']();
                        checkCode.getParent()[showCheckCode ? 'show' : 'hide']();
                        // 控制 税前金额 显示
                        preTaxAmount[showPreTaxAmount ? 'enable' : 'disable']();
                        preTaxAmount.getParent()[showPreTaxAmount ? 'show' : 'hide']();
                        // 控制 金额 显示
                        amount[showAmount ? 'enable' : 'disable']();
                        amount.getParent()[showAmount ? 'show' : 'hide']();
                        // 控制 金额 显示
                        tax[showTax ? 'enable' : 'disable']();
                        tax.getParent()[showTax ? 'show' : 'hide']();
                    },
                    init: function (event) {
                        mao.ui.SelectEdit.prototype.init.call(this, event);
                        util.timer(function () {
                            this.changeHandler(event);
                        }, 30, this);
                    }
                }
            ),
            SaveInvoiceBtn: ecui.inherits(
                ui.Control,
                function (el, options) {
                    ui.Control.call(this, el, options);
                    this._bAgain = options.again;
                },
                {
                    onclick: function () {
                        var data = {};
                        ecui.esr.parseObject(document.forms.adjustInvoiceForm, data, false);
                        this.request(
                            'data@FORM /erp-management/expense/ocr/invoice/save?adjustInvoiceForm',
                            function () {
                                var code = ecui.esr.getData('data_CODE');
                                if (code === 0) {
                                    if (fromEdit === 'true') {
                                        ecui.dispatchEvent(ecui.esr.findRoute(this).owner, 'change', { item: Object.assign(detail, data) });
                                    }
                                    history.go(-1);
                                }
                            }.bind(this),
                            function () {
                                return false;
                            }
                        );
                    }
                },
                ui.Submit
            )
        }
    );
    ecui.esr.addRoute('adjust', {
        model: function (context, callback) {
            var model = [];
            if (context.id) {
                model.push('detail@GET /erp-management/expense/ocr/invoice/detail/${id}');
            }
            ecui.esr.request(model, callback, function () {
                return true;
            });
            return false;
        },
        weight: 4,
        frame: true,
        onbeforerequest: function (context) {
            context.num = mao.util.getTimeRandom();
            fromEdit = context.fromEdit;
        },
        onbeforerender: function (context) {
            detail = context.detail;
        },
        onafterrender: function (context) {
        }
    });
}());

